﻿#ifndef __HTTP_CLIENT_H__
#define __HTTP_CLIENT_H__
#include "../iocp/IOCP/iocpdef.h"
#include <map>
#include <string>

#define HTTP_CLOSETIME   7 //等待7秒后再对链接进行关闭,可以减少TIME_WAIT状态

using http_read_complete_cb = void(__stdcall *)(struct __HTTPINFO* pHttpInfo);
using http_write_complete_cb = void(__stdcall *)(struct __HTTPINFO* pHttpInfo);
using http_accept_complete_cb = void(__stdcall *)(struct __HTTPSERVER* pHttpServer, struct __HTTPINFO* pHttpInfo);
using http_pathhandle_cb = void(__stdcall *)(struct __HTTPINFO* pHttpInfo); //访问某个路径是产生的回调函数

typedef struct __HTTPINFO
{
    HIOCPFILE hHttp;
    struct __HTTPSERVER* pHttpServer;
    void* pUserData;
    char Buffer[4 * 1024];
    char strModle[8]; //Get or Post 等方法
    int nBufferOffset; //实际缓冲长度
    int nCheckOffset; //已经进行Http检查的长度
    const char* strPath; //路径在Buffer缓冲中的开始地址
    int nPathSize; //路径字符串的长度
    const char* strParam; //http参数在Buffer缓冲中的开始地址
    int nParamSize; //http参数在Buffer缓冲中的长度
    int nContentLength; //body报文数据长度
    BOOL bKeepAlive; //http链接保持活跃
    BOOL bParser; //已经进行头部检查
} HTTPINFO, *PHTTPINFO;


typedef struct __HTTPSERVER
{
    HIOCPFILE hObject;
    http_pathhandle_cb ErrorHandle;
    std::map<std::string, http_pathhandle_cb> PathHandles;
} HTTPSERVER, *PHTTPSERVER;

PHTTPSERVER http_listen(HIOCPBASE hIocp);
void http_listen_close(PHTTPSERVER pHttpServer);
BOOL http_register_pathcall(PHTTPSERVER pHttpServer, const char* strPath, http_pathhandle_cb pfnPathHandle);
BOOL http_register_errorcall(PHTTPSERVER pHttpServer, http_pathhandle_cb pfnPathHandle);
void http_close(HTTPINFO* pHttp, DWORD dwSecond);

void websvr_read_complete_callback(struct __HTTPINFO* pHttpInfo);
void websvr_write_complete_callback(struct __HTTPINFO* pHttpInfo);
void websvr_accept_complete_callback(struct __HTTPSERVER* pHttpServer, struct __HTTPINFO* pHttpInfo);

#endif
